package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.vo.ProductVo;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
@Tag(name = "商品管理模块")
public class ProductController extends BaseController {
    @Resource
    private ProductService productService;

    @Operation(summary = "条件分页查询商品列表")
    @GetMapping("/list")
    @RequiresPermissions("product:product:list")
    public TableDataInfo list(Product product) {
        startPage();
        List<Product> productList = productService.getProductList(product);
        return getDataTable(productList);
    }

    @Operation(summary = "根据商品ID查询商品")
    @GetMapping("/{id}")
    @RequiresPermissions("product:product:info")
    public AjaxResult getProductById(@PathVariable Long id) {
        ProductVo productVo = productService.getProductById(id);
        return success(productVo);
    }

    @Operation(summary = "新增|修改商品")
    @PostMapping("/saveOrUpdate")
    @RequiresPermissions("product:product:saveOrUpdate")
    public AjaxResult saveOrUpdateProduct(@RequestBody ProductVo productVo) {
        return toAjax(productService.saveOrUpdateProduct(productVo));
    }

    @Operation(summary = "根据商品id删除商品")
    @DeleteMapping("/{id}")
    @RequiresPermissions("product:product:remove")
    public AjaxResult deleteProductByIds(@PathVariable Long id) {
        return toAjax(productService.deleteProductByIds(id));
    }

    @Operation(summary = "根据商品id修改商品审核状态")
    @PostMapping("/updateAuditStatus/{id}/{auditStatus}")
    @RequiresPermissions("product:product:updateAuditStatus")
    public AjaxResult updateProductAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        return toAjax(productService.update(new LambdaUpdateWrapper<Product>().eq(Product::getId, id)
                .set(Product::getAuditStatus, auditStatus)
                .set(Product::getUpdateBy, SecurityUtils.getUsername())
                .set(Product::getAuditMessage, auditStatus == 1 ? "审核通过" : "审核不通过")));
    }

    @Operation(summary = "根据商品id修改商品线上状态")
    @PutMapping("/updateStatus/{id}/{status}")
    @RequiresPermissions("product:product:updateStatus")
    public AjaxResult updateProductStatus(@PathVariable Long id, @PathVariable Integer status) {
        return toAjax(productService.updateProductStatus(id, status));
    }
}